﻿using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using DocumentFormat.OpenXml.Wordprocessing;
using WordDocumentGenerator.Library;
using System.Reflection;

namespace GPS.COMMON
{
    public class ExportCore<T> : DocumentGenerator
    {


        #region "Constructor"

        public ExportCore(DocumentGenerationInfo generationInfo)
            : base(generationInfo)
        {
        }

        #endregion

        #region "Overridden methods"

        /// <summary>
        /// Gets the place holder tag to type collection.
        /// </summary>
        /// <returns></returns>
        protected override Dictionary<string, PlaceHolderType> GetPlaceHolderTagToTypeCollection()
        {
            Dictionary<string, PlaceHolderType> placeHolderTagToTypeCollection = new Dictionary<string, PlaceHolderType>();
            T objT = Activator.CreateInstance<T>();
            Type type = objT.GetType();
            string typeName = type.FullName;
            PropertyInfo[] lstPis = type.GetProperties();
            foreach (PropertyInfo pi in lstPis)
            {
                if (!pi.Name.Trim().StartsWith("LIST"))
                {
                    placeHolderTagToTypeCollection.Add(pi.Name, PlaceHolderType.NonRecursive);
                }
                else
                {
                    placeHolderTagToTypeCollection.Add(pi.Name, PlaceHolderType.Recursive);
                }
            }

            return placeHolderTagToTypeCollection;
        }

        /// <summary>
        /// Ignore placeholder found.
        /// </summary>
        /// <param name="placeholderTag">The placeholder tag.</param>
        /// <param name="openXmlElementDataContext">The open XML element data context.</param>
        protected override void IgnorePlaceholderFound(string placeholderTag, OpenXmlElementDataContext openXmlElementDataContext)
        {
        }

        /// <summary>
        /// Export từng thuộc tính
        /// Non recursive placeholder found.
        /// </summary>
        /// <param name="placeholderTag">The placeholder tag.</param>
        /// <param name="openXmlElementDataContext">The open XML element data context.</param>

        protected override void NonRecursivePlaceholderFound(string placeholderTag, OpenXmlElementDataContext openXmlElementDataContext)
        {
            if (openXmlElementDataContext == null || openXmlElementDataContext.Element == null || openXmlElementDataContext.DataContext == null)
            {
                return;
            }

            string tagPlaceHolderValue = string.Empty;
            string tagGuidPart = string.Empty;
            GetTagValue(openXmlElementDataContext.Element as SdtElement, out tagPlaceHolderValue, out tagGuidPart);

            string tagValue = string.Empty;
            string content = string.Empty;
            T objT = Activator.CreateInstance<T>();
            if (openXmlElementDataContext.DataContext != null)
            {
                objT = (T)Convert.ChangeType(openXmlElementDataContext.DataContext, typeof(T));
            }
            PropertyInfo[] lstPis = objT.GetType().GetProperties();
            foreach (PropertyInfo pi in lstPis)
            {
                if (!pi.Name.Trim().StartsWith("LIST"))
                {
                    if (pi.Name == tagPlaceHolderValue)
                    {
                        if (pi.GetValue(objT, null) != null)
                        {
                            content = pi.GetValue(objT, null).ToString();
                        }
                        else
                        {
                            //openXmlElementDataContext.Element.Remove()
                            content = string.Empty;
                        }
                    }

                }
            }

            //'Dataset
            DataRow _datarow = null;
            //if (openXmlElementDataContext.DataContext != null)
            //{
            //    _datarow = (DataRow)openXmlElementDataContext.DataContext;
            //}
            //if (_datarow != null)
            //{
            //    foreach (DataColumn cot in _datarow.Table.Columns)
            //    {
            //        if (!cot.ColumnName.Trim().StartsWith("LIST") && cot.ColumnName.Trim().StartsWith("DT_"))
            //        {
            //            if (cot.ColumnName == tagPlaceHolderValue)
            //            {
            //                content = _datarow[cot.ColumnName].ToString();
            //            }
            //            else
            //            {
            //                content = string.Empty;
            //            }
            //        }
            //    }
            //}


            // Set the tag for the content control
            if (!string.IsNullOrEmpty(tagValue))
            {
                this.SetTagValue(openXmlElementDataContext.Element as SdtElement, GetFullTagValue(tagPlaceHolderValue, tagValue));
            }

            // Set text without data binding
            this.SetContentOfContentControl(openXmlElementDataContext.Element as SdtElement, content);
        }

        /// <summary>
        /// Export một danh sách hàng
        /// Recursive placeholder found.
        /// </summary>
        /// <param name="placeholderTag">The placeholder tag.</param>
        /// <param name="openXmlElementDataContext">The open XML element data context.</param>
        protected override void RecursivePlaceholderFound(string placeholderTag, OpenXmlElementDataContext openXmlElementDataContext)
        {
            if (openXmlElementDataContext == null || openXmlElementDataContext.Element == null || openXmlElementDataContext.DataContext == null)
            {
                return;
            }

            string tagPlaceHolderValue = string.Empty;
            string tagGuidPart = string.Empty;
            GetTagValue(openXmlElementDataContext.Element as SdtElement, out tagPlaceHolderValue, out tagGuidPart);

            T objT = Activator.CreateInstance<T>();
            if (openXmlElementDataContext.DataContext != null)
            {
                objT = (T)Convert.ChangeType(openXmlElementDataContext.DataContext, typeof(T));
                PropertyInfo[] lstPis = objT.GetType().GetProperties();
                List<T> lstT = default(List<T>);
                DataSet dsT = default(DataSet);
                foreach (PropertyInfo pi in lstPis)
                {
                    if (pi.Name.Trim().StartsWith("LIST"))
                    {
                        if (pi.Name == tagPlaceHolderValue)
                        {
                            if (pi.GetValue(objT, null) != null)
                            {
                                lstT = (List<T>)pi.GetValue(objT, null);
                                foreach (T oT in lstT)
                                {
                                    SdtElement clonedElement = this.CloneElementAndSetContentInPlaceholders(new OpenXmlElementDataContext
                                    {
                                        Element = openXmlElementDataContext.Element,
                                        DataContext = oT
                                    });
                                }
                                openXmlElementDataContext.Element.Remove();
                            }
                            else
                            {
                                openXmlElementDataContext.Element.Remove();
                            }
                        }

                    }
                    //If Left(pi.Name.Trim(), 2) = "DS" Then
                    //    If pi.Name = tagPlaceHolderValue Then
                    //        If pi.GetValue(objT, Nothing) IsNot Nothing Then
                    //            dsT = DirectCast(pi.GetValue(objT, Nothing), DataSet)
                    //            For Each oT As DataRow In dsT.Tables(0).Rows
                    //                Dim clonedElement As SdtElement = Me.CloneElementAndSetContentInPlaceholders(New OpenXmlElementDataContext() With { _
                    //                .Element = openXmlElementDataContext.Element, _
                    //                .DataContext = oT _
                    //                })
                    //            Next
                    //            openXmlElementDataContext.Element.Remove()
                    //        Else
                    //            openXmlElementDataContext.Element.Remove()
                    //        End If
                    //    End If

                    //End If
                }

            }

        }



        /// <summary>
        /// Container placeholder found.
        /// </summary>
        /// <param name="placeholderTag">The placeholder tag.</param>
        /// <param name="openXmlElementDataContext">The open XML element data context.</param>
        protected override void ContainerPlaceholderFound(string placeholderTag, OpenXmlElementDataContext openXmlElementDataContext)
        {
            if (openXmlElementDataContext == null || openXmlElementDataContext.Element == null || openXmlElementDataContext.DataContext == null)
            {
                return;
            }

            string tagPlaceHolderValue = string.Empty;
            string tagGuidPart = string.Empty;
            GetTagValue(openXmlElementDataContext.Element as SdtElement, out tagPlaceHolderValue, out tagGuidPart);

            string tagValue = string.Empty;
            string content = string.Empty;

            switch (tagPlaceHolderValue)
            {
                //Case PlaceholderContainer_NLC
                //    ' As this sample is non-refreshable hence we don't call GetRecursiveTemplateElementForContainer method( Sets the parentContainer from CustomXmlPart if refresh else saves the parentContainer markup to CustomXmlPart)                

                //    If Not String.IsNullOrEmpty(tagValue) Then
                //        Me.SetTagValue(TryCast(openXmlElementDataContext.Element, SdtElement), GetFullTagValue(tagPlaceHolderValue, tagValue))
                //    End If

                //    For Each v In openXmlElementDataContext.Element.Elements()
                //        Me.SetContentInPlaceholders(New OpenXmlElementDataContext() With { _
                //         .Element = v, _
                //         .DataContext = openXmlElementDataContext.DataContext _
                //        })
                //    Next

                //    Exit Select
            }
        }

        #endregion
    }

}
